/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.Action;
import com.vng.zing.survey.thrift.model.IDUserResponseItem;
import com.vng.zing.survey.thrift.model.Error;

import com.vng.zing.survey.thrift.model.IDUserResQuesNoRequiKey;
import java.io.IOException;
import java.util.ArrayList;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import kyotocabinet.DB;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class IDUserResponseData extends Base {
    private static final Logger logger_ = Logger.getLogger(IDUserResponseData.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static IDUserResponseData _instance = null;
    private final DB db;

    public IDUserResponseData() {
        db = new DB();
        if (!db.open("database/iduserresponse.kch"+cofigid, DB.OWRITER | DB.OCREATE |DB.OAUTOTRAN)) {
            System.err.println("open error: " + db.error());
        }
    }

    public static IDUserResponseData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new IDUserResponseData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public Error setIDUserResponseItem(int idSurvey, IDUserResponseItem dUserResponseItem) {
        try {
            byte[] key = serialize(idSurvey);
            byte[] value = serialize(dUserResponseItem);
            if (!db.set(key, value)) {

                return Error.ERR_SYSTEM;
            }
            return Error.SUCCESS;
        } catch (IOException ex) {
            logger_.error("IDUserResponseData.setIDUserResponseItem:" + ex.getMessage(), ex);
        }
        return Error.ERR_SYSTEM;
    }

    public IDUserResponseItem getIDUserResponseItem(int idSurvey) {
        try {
            byte[] key = serialize(idSurvey);
            byte[] value = db.get(key);
            if (value == null) {
                return null;
            }
            IDUserResponseItem dUserResponseItem = new IDUserResponseItem();
            dUserResponseItem = (IDUserResponseItem) deserialize(value);
            return dUserResponseItem;

        } catch (IOException ex) {
            logger_.error("IDUserResponseData.getIDUserResponseItem:" + ex.getMessage(), ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public Error removeResponseItem(int idSurvey) {
        try {
            byte[] key = serialize(idSurvey);
            if (!db.remove(key)) {
                return Error.ERR_SYSTEM;
            }
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
        }

        return Error.SUCCESS;
    }

   

    public Error updateIDUserResponseItem(int idSurvey, int idUser, Action action) {
      
        db.begin_transaction(false);
            IDUserResponseItem dUserResponseItem = new IDUserResponseItem(new ArrayList<Integer>(10000));
            byte[] key = null;
            byte[] value = null;
            try {
                key = serialize(idSurvey);
                value = db.get(key);
                if (value != null) {
                    dUserResponseItem = (IDUserResponseItem) deserialize(value);
                }
                if (action == Action.ADD) {
                    dUserResponseItem.getIdUsers().add(0, idUser);
                } else if (action == Action.REMOVE) {
                    dUserResponseItem.getIdUsers().remove((Integer) idUser);
                }
                value = serialize(dUserResponseItem);
                if (!db.set(key, value)) {
                      db.end_transaction(true);   
                    return Error.ERR_SYSTEM;
                }
                     db.end_transaction(true);
                return Error.SUCCESS;

            } catch (IOException ex) {
                logger_.error("IDUserResponseData.updateIDUserResponseItem:" + ex.getMessage(), ex);
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
            }
          db.end_transaction(true);
        return Error.ERR_SYSTEM;

    }

   
}
